package pl.koziolekweb.propertiestranslator.maven;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.model.FileSet;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import pl.koziolekweb.translator.engine.Language;
import pl.koziolekweb.translator.engine.Translator;
import pl.koziolekweb.translator.engine.TranslatorServiceFactory;
import pl.koziolekweb.translator.engine.googleimpl.GoogleLanguage;
import pl.koziolekweb.translator.engine.googleimpl.TranslatorImpl;

/**
 * Goal which translate all <samp>.properties</samp> files from
 * ${project.resources} directory or if you set <samp>propertiesDir<samp> then
 * goal search and translate files from this directory.
 * 
 * @goal translate
 * 
 * @phase compile
 */
public class PropertiesTranslatorMojo extends AbstractMojo {

	/**
	 * Default location of <samp>.properties</samp> files.
	 * 
	 * @parameter expression="${project.resources.dirctory}"
	 * @required false
	 */
	private FileSet propertiesList;

	/**
	 * Default language.
	 * 
	 * @parameter default-value="ENGLISH"
	 * @required false
	 */
	private String sourceLanguage;

	/**
	 * Target languages.
	 * 
	 * @parameter
	 * @required false
	 */
	private String targetLanguages;

	private File directory;

	public void execute() throws MojoExecutionException {
		if (!prepare()) {
			return;
		}
		List<Properties> properties = searchProperties();
		getLog().info("Source language is: " + sourceLanguage);
		getLog().info("target languages are: ");
		for (String s : targetLanguages.split(",")) {
			getLog().info(s);
		}

		Translator translator = new TranslatorImpl();
		Language sLanguage = GoogleLanguage.valueOf(sourceLanguage.toUpperCase());
		for (String s : targetLanguages.split(",")) {
			Properties resultProp = new Properties();
			if (translator.getSupportedLanguages().contains(s)) {
				Language language = GoogleLanguage.valueOf(s);
				
				for(Properties p : properties){
					Set<Object> keys = p.keySet();
					for(Object key: keys){
						try {
							resultProp.put(key, translator.translate(p.get(key).toString(), sLanguage, language));
						} catch (Throwable e) {
							e.printStackTrace();
						}
					}
				}
				
			} else {
				getLog().info("Language " + s + " is not supportet.");
			}
			Set<Object> keys = resultProp.keySet();
			for(Object key: keys){
				getLog().info("translation: " + key + " : "+ resultProp.get(key));
			}
		}
	}

	private List<Properties> searchProperties() {
		List<Properties> properties = new LinkedList<Properties>();
		File[] files = directory.listFiles(new FileFilter() {

			private String getFileExtension(File pathname) {
				String ex;
				String name = pathname.getName();
				String[] sp = name.split("\\.");

				ex = sp[sp.length - 1];

				return ex;
			}

			@Override
			public boolean accept(File pathname) {
				String s = getFileExtension(pathname);
				if ("properties".equals(s)) {
					return true;
				}
				return false;
			}
		});

		for (File f : files) {
			String s = f.getName().split("\\.")[0];
			if (s.split("_").length == 1) {
				Properties properties2 = new Properties();
				try {
					properties2.load(new FileInputStream(f));
					getLog().info("Add file " + f.getName() + " to translation list");
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return properties;
	}

	private boolean prepare() {
		System.setProperty("run_as_maven", "true");
		directory = new File(propertiesList.getDirectory());
		if (!directory.isDirectory()) {
			getLog().error(propertiesList.getDirectory() + " is not directory!");
			return false;
		}

		return true;
	}
}
